﻿#region usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
#endregion

namespace InsuranceAdjuster.Controls {
    public partial class StreamImage : UserControl {
        public static readonly DependencyProperty ImageDataProperty = DependencyProperty.Register(
            "ImageData"
            , typeof(byte[])
            , typeof(FrameworkElement)
            , new PropertyMetadata(null, new PropertyChangedCallback(ImageDataChanged)));

        public byte[] ImageData {
            get { return (byte[])GetValue(ImageDataProperty); }
            set { SetValue(ImageDataProperty, value); }
        }

        public StreamImage() {
            InitializeComponent();
        }
        /*
         var bs = new BitmapSource();
            bs.SetSource(new MemoryStream(bytes));
            new Image().Source = bs;
         */

        private static void ImageDataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
            var ThisControl = sender as StreamImage;

            
            
            if (e.NewValue == null)
                ThisControl.DisplayImage.Source = null;
            else {
                var MyStream = new MemoryStream(e.NewValue as byte[]);
                BitmapImage MyImage = new BitmapImage();
                MyImage.SetSource(MyStream);
                MyStream.Close();
                ThisControl.DisplayImage.Source = MyImage;
            }

        }
    }
}
